﻿#include "UScenePage.h"
#include "Global.h"
#include <QSplitter>
UScenePage::UScenePage(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	QList<int> sizes;
	sizes << 1000 << 300; 
	ui.splitter->setSizes(sizes);
}

UScenePage::~UScenePage()
{

}

void UScenePage::setRootNode(CBaseNode * pRoot)
{
	initUi();
}

UDropCellWidget * UScenePage::getDropWidget()
{
	return ui.widget;
}

void UScenePage::initUi()
{
	/**
	* @ Author: Gaojia
	* @ Description:
	*/
	ui.scrollArea->setWidgetResizable(true);  // 允许自动调整大小
	ui.scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

	/**
	* @ Author: Gaojia
	* @ Description: 初始化设备树
	*/
	connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboxChanged(int)));
	buildComBox();
	
	ui.splitter->widget(0)->setMinimumWidth(20); // 左边最小 200
	ui.splitter->widget(1)->setMinimumWidth(20); // 右边最小 300
	ui.splitter->setCollapsible(0, false);
	ui.splitter->setCollapsible(1, false);
}

void UScenePage::buildComBox()
{
	ui.comboBox->blockSignals(true);
	ui.comboBox->clear();
	ui.comboBox->addItem("*");
	for (auto it = g_mapCardWidget.begin(); it != g_mapCardWidget.end(); it++)
	{
		QString sTypeName = g_mapDllName.begin().key();
		for (auto i = g_mapDllName.begin(); i != g_mapDllName.end(); i++)
		{
			if (i.value() == it.key())
			{
				sTypeName = i.key();
			}
		}
		for (auto itType = it.value().begin(); itType != it.value().end(); itType++)
		{
			ui.comboBox->addItem(sTypeName);
			//ui.comboBox->addItem(itType.value().sTypeName);
			int index = ui.comboBox->count() - 1;
			// 存放驱动名称
			ui.comboBox->setItemData(index, sTypeName, Qt::UserRole);
			// 存放设备名称
			ui.comboBox->setItemData(index, QVariant::fromValue(reinterpret_cast<void*>(&itType.value())), Qt::UserRole + 1);
		}
	}
	ui.comboBox->blockSignals(false);
	slot_comboxChanged(0);
}

void UScenePage::clearScrollArea()
{
	// 1. 获取 scrollArea 中当前设置的中心控件
	QWidget* oldWidget = ui.scrollArea->widget();
	// 2. 如果存在旧控件，先移除并删除
	if (oldWidget) 
	{
		ui.scrollArea->takeWidget();  // 从 scrollArea 中移除
		delete oldWidget;              // 删除旧控件
	}
}

void UScenePage::initComBoxItem(QVBoxLayout *pLayout,int nIndex)
{
	SDeviceWidget* pDevice = reinterpret_cast<SDeviceWidget*>(ui.comboBox->itemData(nIndex, Qt::UserRole + 1).value<void*>());
	if (!pDevice) return;
	QVector<QWidget *>vecWidget = pDevice->vecWidget;
	if (vecWidget.size() > 0)
	{
		for (int i = 0; i < vecWidget.size(); i++)
		{
			CDragPreviewWidget *pDrag = new CDragPreviewWidget("", QString::number(i), pDevice->sDllName, pDevice->nDeviceType, vecWidget[i], -1, this);
			pLayout->addWidget(pDrag);
			pDrag->setFixedSize(480, 246);  // 宽120，高80
		}
		pLayout->addStretch();
	}
}

void UScenePage::slot_comboxChanged(int nIndex)
{
	clearScrollArea();
	//创建新的控件并设置进去
	QWidget* newWidget = new QWidget();
	ui.scrollArea->setWidget(newWidget);
	QVBoxLayout *pLayout = new QVBoxLayout(newWidget);
	pLayout->setContentsMargins(5, 5, 5, 5);
	pLayout->setSpacing(40);
	if (nIndex == 0)
	{
		int nCount = ui.comboBox->count();
		for (int i = 1; i < nCount; i++)
		{
			initComBoxItem(pLayout, i);
		}
	}
	else
	{
		initComBoxItem(pLayout,nIndex);
	}
}



void UScenePage::onTreeClickeTemple(const QModelIndex & index)
{
	if (!index.isValid()) return;
	QStandardItem* item = m_pModel->itemFromIndex(index);
	if (!item) return;
	SDeviceWidget* pDevice = reinterpret_cast<SDeviceWidget*>(item->data(Qt::UserRole+1).value<void*>());
	if (!pDevice) return;

	QVector<QWidget *>vecWidget = pDevice->vecWidget;
	if (vecWidget.size() > 0)
	{
		clearScrollArea();
		//创建新的控件并设置进去
		QWidget* newWidget = new QWidget();
		ui.scrollArea->setWidget(newWidget);
		QVBoxLayout *pLayout = new QVBoxLayout(newWidget);
		pLayout->setContentsMargins(5, 5, 5, 5);
		pLayout->setSpacing(10);
		for (int i = 0; i < vecWidget.size(); i++)
		{
			CDragPreviewWidget *pDrag = new CDragPreviewWidget("", QString::number(i), pDevice->sDllName, pDevice->nDeviceType, vecWidget[i],0, this);
			pLayout->addWidget(pDrag);
			pDrag->setFixedSize(330, 150);  // 宽120，高80
		}
		pLayout->addStretch();
	}
}

/**
* @ Author: Gaojia
* @ Description:  原来设备列表点击树响应
*/
//void UScenePage::onTreeClickeTemple(const QModelIndex & index)
//{
//	if (!index.isValid()) return;
//	QStandardItem* item = m_pModel->itemFromIndex(index);
//	if (!item) return;
//	CBaseNode* node = reinterpret_cast<CBaseNode*>(item->data(Qt::UserRole).value<void*>());
//	if (!node) return;
//	if (node->getType() == CBaseNode::enDevice)
//	{
//		CBaseDevice *pDevice = (CBaseDevice *)node;
//		QVector<QWidget *>vecWidget = pDevice->getDevice()->getDeviceUiList();
//		if (vecWidget.size() > 0)
//		{
//			clearScrollArea();
//			//创建新的控件并设置进去
//			QWidget* newWidget = new QWidget();
//			ui.scrollArea->setWidget(newWidget);
//			QVBoxLayout *pLayout = new QVBoxLayout(newWidget);
//			pLayout->setContentsMargins(5, 5, 5, 5);
//			pLayout->setSpacing(10);
//			for (int i = 0; i < vecWidget.size(); i++)
//			{
//				CDragPreviewWidget *pDrag = new CDragPreviewWidget(pDevice->getId(), QString::number(i), pDevice->getDllName(),pDevice->getDeviceType(), vecWidget[i], this);
//				pLayout->addWidget(pDrag);
//				pDrag->setFixedSize(300, 120);  // 宽120，高80
//			}
//		}
//
//	}
//}
